﻿using System;
using System.Collections.Generic;
using System.Xml;
using DarkAngels.WoW.Library.Interfaces;
using enums = DarkAngels.WoW.Library.Armory;

namespace DarkAngels.WoW.Library.Armory.Realm
{
    public struct Realm
    {
        public RealmType Type
        {
            get;
            internal set;
        }

        public enums.RealmStatus Status
        {
            get;
            internal set;
        }

        public RealmPopulation Population
        {
            get;
            internal set;
        }

        public RealmLanguage Language
        {
            get;
            internal set;
        }

        public String Name
        {
            get;
            internal set;
        }
    }

    /// <summary>
    /// Downloads the list of realm status information and returns an array of realms.
    /// </summary>
    public class RealmStatus : AbstractDataClass
    {
        public RealmStatus(ArmoryRegion region, INetworking provider)
        {
            Region = region;
            Provider = provider;
        }

        public RealmStatus(ArmoryRegion region) : this(region, new DarkAngels.WoW.Library.Network.SimpleNetworking()) { }
        public RealmStatus() : this(ArmoryRegion.EU, new DarkAngels.WoW.Library.Network.SimpleNetworking()) { }

        public List<Realm> Realms
        {
            get;
            private set;
        }

        private String XmlData
        {
            get;
            set;
        }

        public override void Load()
        {
            if (Region == ArmoryRegion.EU || Region == ArmoryRegion.US)
                XmlData = Provider.GetFile(BuildSpecialUri("index.xml"));
            if (XmlData.Length > 0)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(XmlData);
                XmlNodeList items = xmlDoc.SelectNodes("/rss/channel/item");
                BuildRealmList(items);
            }
            else
                throw new NotImplementedException("Currently, only the following regions are implemented: EU, US");
        }

        private String BuildSpecialUri(String fileName)
        {
            string finalUri = String.Format("[BASE]/{0}", fileName);
            switch (Region)
            {
                case ArmoryRegion.EU:
                    finalUri = finalUri.Replace("[BASE]", "http://www.wow-europe.com/realmstatus");
                    break;
                case ArmoryRegion.US:
                    finalUri = finalUri.Replace("[BASE]", "http://www.worldofwarcraft.com/realmstatus");
                    break;
                case ArmoryRegion.KR:
                    throw new ArgumentException("This region has not been implemented yet");
                case ArmoryRegion.TW:
                    throw new ArgumentException("This region has not been implemented yet");
                case ArmoryRegion.CN:
                    throw new ArgumentException("This region has not been implemented yet");
                default:
                    finalUri = finalUri.Replace("[BASE]", "http://www.wow-europe.com/realmstatus");
                    break;
            }
            finalUri = finalUri.Replace(' ', '+');
            return finalUri;
        }

        private void BuildRealmList(XmlNodeList list)
        {
            List<Realm> realms = new List<Realm>();
            bool skipFirst = this.Region == ArmoryRegion.EU;
            foreach (XmlNode node in list)
            {
                if (!skipFirst)
                    realms.Add(BuildRealmItem(node));
                skipFirst = false;
            }
            Realms = realms;
        }

        private Realm BuildRealmItem(XmlNode node)
        {
            Realm r = new Realm();
            if (Region == ArmoryRegion.EU)
            {
                r.Name = node.SelectSingleNode("./title").InnerText;
                foreach (XmlNode n in node.SelectNodes("./category"))
                {
                    foreach (XmlAttribute attr in n.Attributes)
                    {
                        if (attr.Name.Equals("domain"))
                        {
                            if (attr.Value.ToLower().Equals("status"))
                            {
                                r.Status = StringToRealmStatus(n.InnerText);
                            }
                            else if (attr.Value.ToLower().Equals("language"))
                            {
                            }
                            else if (attr.Value.ToLower().Equals("type"))
                            {
                            }
                            else if (attr.Value.ToLower().Equals("population"))
                            {
                            }
                            else if (attr.Value.ToLower().Equals("queue"))
                            {
                            }
                        }
                    }
                }
            }
            else if (Region == ArmoryRegion.US)
            {
                String s = node.SelectSingleNode("./title").InnerText;
                String[] parts = s.Split((new char[] { '(', ')' }));
                r.Name = parts[0].Trim();
                r.Status = StringToRealmStatus(parts[parts.Length - 1].Trim());
            } //add more when other regions get their own status pages
            return r;
        }

        private enums.RealmStatus StringToRealmStatus(String s)
        {
            if (s.ToLower().Trim().IndexOf("up") > -1)
                return enums.RealmStatus.Up;
            else
                return enums.RealmStatus.Down;
        }
    }
}
